﻿using System;
using System.IO;
using System.Reflection;

namespace Alfa.Common {
	public class AssemblyInfo {
		private Assembly AssemblyInstance;

		public AssemblyInfo( ) {
			this.AssemblyInstance = Assembly.GetCallingAssembly( );
		}

		public AssemblyInfo(Assembly assemblyInstance)
			: this( ) {
			if (assemblyInstance != null)
				this.AssemblyInstance = assemblyInstance;
		}

		/// <summary>
		/// Field AssemblyTitle
		/// </summary>
		public string Title {
			get {
				object[ ] attributes = this.AssemblyInstance.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
				if (attributes.Length == 0) {
					return System.IO.Path.GetFileNameWithoutExtension(this.AssemblyInstance.CodeBase);
				}
				return ((AssemblyTitleAttribute)attributes[0]).Title;

			}
		}

		/// <summary>
		/// Field AssemblyDescription
		/// </summary>
		public string Description {
			get {
				object[ ] attributes = this.AssemblyInstance.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
				if (attributes.Length == 0) {
					return "";
				}
				return ((AssemblyDescriptionAttribute)attributes[0]).Description;
			}
		}

		/// <summary>
		/// Field AssemblyCompany
		/// </summary>
		public string Company {
			get {
				object[ ] attributes = this.AssemblyInstance.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
				if (attributes.Length == 0) {
					return "";
				}
				return ((AssemblyCompanyAttribute)attributes[0]).Company;
			}
		}

		/// <summary>
		/// Field AssemblyProduct
		/// </summary>
		public string Product {
			get {
				object[ ] attributes = this.AssemblyInstance.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
				if (attributes.Length == 0) {
					return "";
				}
				return ((AssemblyProductAttribute)attributes[0]).Product;
			}
		}

		/// <summary>
		/// Field AssemblyCopyright
		/// </summary>
		public string Copyright {
			get {
				object[ ] attributes = this.AssemblyInstance.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
				if (attributes.Length == 0) {
					return "";
				}
				return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
			}
		}

		/// <summary>
		/// Field AssemblyVersion
		/// </summary>
		public Version Version {
			get {
				AssemblyName assemblyName = new AssemblyName(this.AssemblyInstance.FullName);

				return assemblyName.Version;
			}
		}

		/// <summary>
		/// Contains date and time of release application, computed from automatic AssemblyVersion (x.x.*) eg. 1.0.*
		/// </summary>
		public DateTime ReleasionDate {
			get {
				AssemblyName assemblyName = new AssemblyName(this.AssemblyInstance.FullName);

				return new DateTime(2000, 1, 1).AddDays(assemblyName.Version.Build).AddSeconds(assemblyName.Version.Revision * 2).AddHours(-1);
			}
		}

		/// <summary>
		/// Contaions FileInfo about current exacutable file path
		/// </summary>
		public FileInfo ExecutablePath {
			get {
				return new FileInfo(this.AssemblyInstance.Location);
			}
		}
	}
}
